#include "interact.h"
#include "userLogin.h"

int main()
{
    //与客户端建立连接
    int ret;
    char argv[2][ARGS_LENGTH] = {0};
    ret = read_config(NETCONF_FILENAME,argv,2);
    if(ret == -1)
    {
        fprintf(stderr,"shell :err in read_config\n");
        return -1;
    }

    int socketFd;
    socketFd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socketFd,-1,"socket in shell");
    printf("connecting to server...socketFd: %d\n",socketFd);
    struct sockaddr_in ser;
    bzero(&ser,sizeof(ser));
    ser.sin_family=AF_INET;
    ser.sin_port=htons(atoi(argv[1]));
    ser.sin_addr.s_addr=inet_addr(argv[0]);
    ret=connect(socketFd,(struct sockaddr*)&ser,sizeof(ser));
    ERROR_CHECK(ret,-1,"connect");
    printf("connected , socketFd :%d\n",socketFd);
    //登陆功能,暂时只有登陆，没有新建用户，循环登录直到成功为止

    userStat_t user = {0};
    while(1)
    {
        if(0 == userLogin(socketFd,&user))
        {
            user.soecktFd = socketFd;
            strcpy(user.pwd, "/");
            break;
        }
    }

    //服务器负载允许
    int connectionAllow = 0;
    recvCyc(socketFd, &connectionAllow, sizeof(int));//接收服务器登陆允许信息
    //printf("recvCyc: connectionAllow:%d\n", connectionAllow);
    if(!connectionAllow)
    {
        printf("connect failed,return connectionAllow = %d,please try again later,byebye\n",connectionAllow);
        goto end_lable;
    }
    printf("server connected: IP:%s PORT:%s\n", argv[0], argv[1]);

    //进入shell
    shell(&user);

end_lable:
    close(socketFd);
    return 0;
}

int get_args(char args[][ARGS_LENGTH], int *pargc)//注意这个形参args的形式 写成char**会警告
{
    int i = 0;
    int argc = 0;
    char tmp;
    read(STDIN_FILENO, &tmp, sizeof(char));
    while (tmp != '\n')
    {
        i = 0;
        while (' ' != tmp && '\t' != tmp&&'\n'!=tmp)
        {
            args[argc][i++] = tmp;
            read(STDIN_FILENO, &tmp, sizeof(char));
        }
       // printf("get a arg: %s,argc: %d\n", args[argc], argc + 1);
        argc++;

        if(argc>ARGS_NUM)
        {
            fprintf(stderr,"get_args :too many args! check your ARGS_NUM\n");
            return -1;
        }

        while (' ' == tmp || '\t' == tmp)
        {
            read(STDIN_FILENO, &tmp, sizeof(char));
        }
    }
    *pargc = argc;
    return 0;
}


int read_config(const char *filename,char args[][ARGS_LENGTH],int argNum)
{
    char buf[ARGS_LENGTH] = {0};
    int argc = 0;
    FILE* config = fopen(filename,"rb");
    if(!config)
    {
        fprintf(stderr,"error in main: config open failed\n");
    }
    while(argc<argNum)
    {
        bzero(&buf,sizeof(buf));
        fgets(buf,ARGS_LENGTH,config);
        if(feof(config))
            break;
        if(buf[0] == '#'||buf[0] == '\n'||buf[0] == 0)
            continue;
        else
        {
            strcpy(args[argc],buf);
            args[argc][strlen(buf)-1] = 0;//删除换行符号
            argc++;
        }
    }
    if(argc == argNum)
        return argc;
    else
    {
        while(argc--)
        {
            printf("%s\n", args[argc]);
        }
        fprintf(stderr,"read_config :wrong num of args\n");
        return -1;
    }
}




int shell(puserStat puser)
{
    printf("hello !this is myCloud shell:\n");
    //初始化命令表，采用顺序表存储查询
    command_t cmds[CMD_TYPES] = {
        {"pwd",cmd_pwd},
        {"ls",cmd_ls},
        {"cd",cmd_cd},
        {"puts",cmd_puts},
        {"gets",cmd_gets}
        //...
    };

    //进入循环，读取命令，解析命令
    int ret = 0;
    char args[ARGS_NUM][64] = {0};
    int argc = 0;
    while(1)
    {
        printf("[%s @myCloud: %s]$",puser->name,puser->pwd);//提示符
        fflush(stdout);
        bzero(args, sizeof(args)); //初始化变量
        argc = 0;
        ret = get_args(args,&argc);//获取命令
        if(ret == -1)
        {
            fprintf(stderr,"shell :err in get_args");
            return -1;
        }
        printf("argc : %d\n", argc);
        printf("args list:\n");
        for (int i = 0; i < argc; ++i)
        {
            printf("%s\n",args[i]);
        }

        //解析命令
        int cmd_miss = 1;
        for (int i = 0; i < CMD_TYPES; ++i)
        {
            if(0 == strcmp(args[0],cmds[i].command))
            {
                cmd_miss = 0;
                cmds[i].func(argc,args,puser);
            }
        }
        if(cmd_miss)
        {
            printf("shell :command not found :%s \n", args[0]);
        }
    }
}

